﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;

namespace huqiang.Framework.Http
{
    public class HttpDownloader : IMission
    {
        string _url;
        Uri _uri;
        public Uri uri {
            get => _uri; 
        }
        public string url
        {
            get => _url;
            set
            {
                if (_url != value)
                    Length = 0;
                _url = value;
                _uri = new Uri(value);
            }
        }
        public string savePath;
        public int code;
        public bool AutoRetry = true;
        public bool BreakpointResume;
        public int Length;
        public byte[] data;
        public Action<IMission> MainThreadCallback;
        public int DownloadBytes { get {
                if (savePath == null||savePath=="")
                    return 0;
                if (File.Exists(savePath))
                {
                    return (int)new FileInfo(savePath).Length;
                }
                return 0;
            } }
        internal FileInfo fileInfo;
        public override float Progress
        {
            get
            {
                if (Length == 0)
                    return 0;
                return (float)DownloadBytes / (float)Length;
            }
        }
        public override void Reset()
        {
            if (savePath != null && savePath != "")
            {
                if (File.Exists(savePath))
                    File.Delete(savePath);
            }
        }
    }
}